package com.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.common.exception.CatchConsumerException;
import com.admin.mapper.AdSensitiveMapper;
import com.usian.model.admin.dtos.SensitiveDto;
import com.usian.model.admin.pojos.AdSensitive;
import com.usian.model.admin.pojos.AdUser;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.admin.service.AdsensitiveService;
import com.usian.utils.threadlocal.AdminThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;

@Service
public class AdsensitiveImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdsensitiveService {

    @Autowired
    private AdSensitiveMapper am;

    @Override
    public PageResponseResult findAll(SensitiveDto sensitiveDto) {
        // 只用是admin用户才能使用这个方法（只要登录用户才能访问）
        // 1. 解析token  2. 获得用户Id  根据用户Id 查询用户信息
        // 过滤器
        AdUser adUser =  AdminThreadLocalUtils.getUser();
        System.out.println("id是"+adUser.getId()+"-----*****");
        if (sensitiveDto==null){
            CatchConsumerException.catchException(20000,"参数不合法");
        }
        sensitiveDto.checkParam();
        LambdaQueryWrapper<AdSensitive> wrapper = new LambdaQueryWrapper();
        wrapper.like(sensitiveDto.getName()!=null&&!"".equals(sensitiveDto.getName()),AdSensitive::getSensitives,sensitiveDto.getName());
        Page<AdSensitive> page = new Page<>(sensitiveDto.getPage(),sensitiveDto.getSize());
        IPage<AdSensitive> iPage = this.page(page, wrapper);
        PageResponseResult result = new PageResponseResult(iPage.getCurrent(), iPage.getSize(), iPage.getTotal());
        result.setData(iPage.getRecords());
        return result;
    }

    @Override
    public ResponseResult findByIds(Integer id) {
        AdSensitive byId = getById(id);
        return new ResponseResult().ok(byId);
    }

    @Override
    public ResponseResult add(AdSensitive sensitive) {
        sensitive.setCreatedTime(new Date());
        this.save(sensitive);
        return new ResponseResult(200,"成功!!");
    }

    @Override
    public ResponseResult edit(AdSensitive sensitive) {
        this.updateById(sensitive);
        return new ResponseResult(200,"成功!!");
    }

    @Override
    public ResponseResult del(Integer id) {
        this.removeById(id);
        return new ResponseResult(200,"成功!!");
    }

    @Override
    public ResponseResult delAll(Integer[] ids) {
        this.removeByIds(Arrays.asList(ids));
        return new ResponseResult().ok(200,"成功!");
    }

    @Override
    public ResponseResult delByIds(Integer[] ids) {
        am.delByIds(ids);
        return new ResponseResult().ok(200,"成功");
    }

   /* @Override
    public ResponseResult saveSensitive(AdSensitive adSensitive) {
        if(adSensitive==null){
           CatchConsumerException.catchException(20007,"参数不合法");
        }
        adSensitive.setCreatedTime(new Date());
        // sql now()
        this.save(adSensitive);
        System.out.println(adSensitive.getId());
        return ResponseResult.okResult(null);
    }

    @Override
    public ResponseResult findById(Integer id) {
        if(id==null){
            CatchConsumerException.catchException(20008,"Id不能为空");
        }
        AdSensitive adSensitive =  this.getById(id);
        return ResponseResult.okResult(adSensitive);
    }*/
}
